package com.coder.books.thinkinjava.generics;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 *
 * @author yuhushuan
 * @date 2020/8/10 21:10
 */
public class GenericArrayWithTypeToken<T> {
    private T[] array;

    public GenericArrayWithTypeToken(Class<T> type, int sz) {
        array = (T[]) Array.newInstance(type, sz);
    }

    public void put(int index, T item) {
        array[index] = item;
    }

    public T get(int index) {
        return (T) array[index];
    }

    public T[] rep() {
        return (T[]) array;
    }

    public static void main(String[] args) {
        GenericArrayWithTypeToken<Integer> gai =
                new GenericArrayWithTypeToken<>(Integer.class, 10);
        Integer[] ia = gai.rep();
        System.out.println(Arrays.toString(ia));
    }
}
